package com.scau.hwadee.examination.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scau.hwadee.examination.entity.*;
import com.scau.hwadee.examination.mapper.*;
import com.scau.hwadee.examination.service.ExaminationPlanService;
import com.scau.hwadee.examination.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author YHX
 * @date 2019/8/1 16:14
 * description
 */
@Service
public class ExaminationPlanServiceImpl implements ExaminationPlanService {

  @Autowired
  private ExamsessionMapper examsessionMapper;

  @Autowired
  private ExammajorMapper exammajorMapper;

  @Autowired
  private MajorMapper majorMapper;

  @Autowired
  private ExamcourseMapper examcourseMapper;

  @Autowired
  private ExamplanapprovalMapper examplanapprovalMapper;

  @Autowired
  private CourseMapper courseMapper;

  private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

  private SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");

  @Override
  public PageInfo<Examsession> getAllPlanStateExamSession(int pageNum, int pageSize, Integer state) {
    PageHelper.startPage(pageNum, pageSize);
    return new PageInfo<>(examsessionMapper.selectAllExamSessions(pageNum, pageSize, state));
  }

  @Override
  public Map<String, Integer> examinationScheduleProfessionalCourseStatistics(String esId) {
    Map<String, Integer> map = new HashMap<>();
    List<Character> socialProfession = Arrays.asList('A', 'B', 'C', 'D');
    List<Character> appliedProfession = Arrays.asList('Y', 'Z', 'G');
    List<String> bachelor = Arrays.asList("本科段", "独立本科段");
    List<String> specialist = Arrays.asList("专科段");
    map.put("当次课表开放专业数(计重复)", exammajorMapper.selectCountMajorsByExamSessionId(esId));
    map.put("长线专业数(计重复)", exammajorMapper.selectCountMajorIDByExamSessionId(esId, socialProfession));
    map.put("应用型专业数(计重复)", exammajorMapper.selectCountMajorIDByExamSessionId(esId, appliedProfession));
    map.put("本科专业数(计重复)", majorMapper.selectCountMajorTypeByExamSessionId(esId, bachelor));
    map.put("专科专业数(计重复)", majorMapper.selectCountMajorTypeByExamSessionId(esId, specialist));
    map.put("长线专业的课程数(不计重复)", majorMapper.selectCountCourseOfMajorIdByExamSessionId(esId, socialProfession));
    map.put("应用型专业的课程数(不计重复)", majorMapper.selectCountCourseOfMajorIdByExamSessionId(esId, appliedProfession));
    map.put("当次开考的课程总数(不计重复)", examcourseMapper.selectCountExamCourseByExamSessionId(esId));
    return map;
  }

  @Override
  public PageInfo<Examsession> getAllExamsessionWithoutMajorPlan(int pageNum, int pageSize) {
    PageHelper.startPage(pageNum, pageSize);
    return new PageInfo<>(examsessionMapper.selectAllExamSessionsWithoutMajorPlan(pageNum, pageSize));
  }

  @Override
  public int updateExamsession(Examsession examsession) {
    try {
      return examsessionMapper.updateByPrimaryKeySelective(examsession);
    } catch (Exception e) {
      return 0;
    }
  }

  @Override
  public PageInfo<Major> getMajorsByMajorIds(int pageNum, int pageSize, String esId) {
    List<Exammajor> exammajors = exammajorMapper.selectAllExamMajorsByExamSessionId(esId);
    List<String> list = new ArrayList<>();
    for (Exammajor exammajor : exammajors) {
      list.add(exammajor.getMjId());
    }
    PageHelper.startPage(pageNum, pageSize);
    return new PageInfo<>(majorMapper.selectAllMajorByExamSessionId(pageNum, pageSize, list));
  }

  @Override
  public PageInfo<Map<String, Object>> getExamOpeningCourseSummary(int pageNum, int pageSize, String esId) {
    List<Examcourse> examcourses = examcourseMapper.selectAllExamCourseByExamSessionId(1, 100000000, esId);
    return new PageInfo<>(courseMapper.selectExamOpeningCourseSummary(pageNum, pageSize, examcourses));
  }

  @Override
  public List<Examcourse> getExamCourseSummary(int pageNum, int pageSize, String examSessionId) {
    return examcourseMapper.selectAllExamCourseByExamSessionId(pageNum, pageSize, examSessionId);
  }

  @Override
  public PageInfo<Major> getAllMajorByMajorPlanVersionAndMajorState(int pageNum, int pageSize, String majorVersionId, String state) {
    PageHelper.startPage(pageNum, pageSize);
    return new PageInfo<>(majorMapper.selectAllMajorByMajorPlanVersionAndMajorState(pageNum, pageSize, majorVersionId, state));
  }

  @Override
  public List<Examsession> getAllExamSessionWithoutCourse(int pageNum, int pageSize) {
    return examsessionMapper.selectAllExamSessionsWithoutCourse(pageNum, pageSize);
  }

  @Override
  public int createOpeningExamMajor(JSONArray jsonArray) {
    List<Exammajor> exammajors = new ArrayList<>();
    for (int i = 0; i < jsonArray.size(); i++) {
      JSONObject jsonObject = jsonArray.getJSONObject(i);
      String esId = jsonObject.get("examSessionId").toString();
      String mjId = jsonObject.get("majorId").toString();
      Exammajor exammajor = new Exammajor();
      exammajor.setEsId(esId);
      exammajor.setMjId(mjId);
      exammajors.add(exammajor);
    }
    try {
      return exammajorMapper.insertList(exammajors);
    } catch (Exception e) {
      e.printStackTrace();
      return 0;
    }
  }

  @Override
  public int deleteExamMajorList(JSONArray jsonArray) {
    List<String> majorIds = new ArrayList<>();
    for (int i = 0; i < jsonArray.size(); i++) {
      JSONObject jsonObject = jsonArray.getJSONObject(i);
      String mjId = jsonObject.get("majorId").toString();
      majorIds.add(mjId);
    }
    try {
      return exammajorMapper.deleteExamMajorList(majorIds);
    } catch (Exception e) {
      e.printStackTrace();
      return 0;
    }
  }

  @Override
  public int updateExamPlan(Examplanapproval examplanapproval) {
    try {
      return examplanapprovalMapper.updateByPrimaryKey(examplanapproval);
    } catch (Exception e) {
      e.printStackTrace();
      return 0;
    }
  }

  @Override
  public int createExamCourses(JSONArray jsonArray) {
    List<Examcourse> us = new ArrayList<>();
    for (int i = 0; i < jsonArray.size(); i++) {
      Examcourse examcourse = JsonUtils.JSONToObject(jsonArray.getJSONObject(i).toJSONString(), Examcourse.class);
      us.add(examcourse);
    }
    try {
      return examcourseMapper.insertExamCourseList(us);
    } catch (Exception e) {
      e.printStackTrace();
      return 0;
    }
  }

  @Override
  public int updateExamCourseTime(JSONArray jsonArray) {
    List<Examcourse> examcourses = new ArrayList<>();
    try {
      for (int i = 0; i < jsonArray.size(); i++) {
        Examcourse examcourse = JsonUtils.JSONToObject(jsonArray.getString(i), Examcourse.class);
        examcourses.add(examcourse);
      }
      return examcourseMapper.updateExamCourseList(examcourses);
    } catch (Exception e) {
      return 0;
    }
  }

  @Override
  public PageInfo<Examcourse> getAllExamCourseByExamSessionId(int pageNum, int pageSize, String esId) {
    PageHelper.startPage(pageNum, pageSize);
    return new PageInfo<>(examcourseMapper.selectAllExamCourseByExamSessionId(pageNum, pageSize, esId));
  }

  @Override
  public int deleteExamCourseList(JSONArray jsonArray) {
    List<Examcourse> examcourses = new ArrayList<>();
    for (int i = 0; i < jsonArray.size(); i++) {
      Examcourse examcourse = new Examcourse();
      JSONObject data = jsonArray.getJSONObject(i);
      examcourse.setCsId(data.get("csId").toString());
      examcourse.setEsId(data.get("esId").toString());
      examcourses.add(examcourse);
    }
    try {
      return examcourseMapper.deleteExamCourseList(examcourses);
    } catch (Exception e) {
      e.printStackTrace();
      return 0;
    }
  }

  @Override
  public PageInfo<Examplanapproval> getAllExamplanapprovals(int pageNum, int pageSize) {
    PageHelper.startPage(pageNum, pageSize);
    return new PageInfo<>(examplanapprovalMapper.selectAllExamplanapprovals(pageNum, pageSize));
  }


}
